﻿Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing

<Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
Public Class OSDContainer
    'Layout Areas
    Private TLBox As Rectangle
    Private TRBox As Rectangle
    Private BLBox As Rectangle
    Private BRBox As Rectangle
    Private TopBar As Rectangle
    Private BottomBar As Rectangle
    Private LeftBar As Rectangle
    Private RightBar As Rectangle
    'Content Box
    Private MainBox As Rectangle

    Private M_BorderColor As Color = Color.Red
    Private m_BackColor As Color = Color.Black
    Private M_BackOpacity As Integer = 100
    Private M_BorderOpacity As Integer = 100
    Private M_BorderStyle As BorderShapeEnum = BorderShapeEnum.Square
    Private M_BorderWidth As Integer = 2
    Private M_BorderRadius As Integer = 10
    'Private CW As Integer = 1
    Private CH As Integer = 1
    Private M_Border As Integer = 1
    Private M_BarSize_Top As Integer = 16
    Private M_BarSize_Left As Integer = 1
    Private M_BarSize_Right As Integer = 1
    Private M_BarSize_Bottom As Integer = 1
    Private M_Debug As Boolean = False
    Private M_Text As String = "Container"
    Private M_TextColor As Color = Color.Black
    Private M_ShowText As Boolean = True
    Private M_TextSize As Integer = 10
    Private MoveHandle As New Mover
    Public Enum BorderShapeEnum
        Square = 0
        Rounded = 1
    End Enum
    Private Sub OSDContainer_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim G As Graphics = e.Graphics

    End Sub
    Private Sub DrawBackground()
        'Set Size
        SetupSize()

        Dim X, Y, X2, Y2 As Integer

        Dim MainBmp As New Bitmap(Me.Width, Me.Height, Imaging.PixelFormat.Format32bppArgb)
        Dim G As Graphics = Graphics.FromImage(MainBmp)
        Dim TransPen As New Pen(Color.FromArgb(255, 1, 2, 3))
        Dim TransBrush As New SolidBrush(Color.FromArgb(255, 1, 2, 3))

        Dim D_BorderColor As Color = Color.FromArgb((M_BorderOpacity * 2.55), M_BorderColor.R, M_BorderColor.G, M_BorderColor.B)
        Dim D_BackColor As Color = Color.FromArgb(M_BackOpacity, m_BackColor.R, m_BackColor.G, m_BackColor.B)
        G.Clear(Color.Transparent)

        'Create Back BMP
        Dim BackBmp As New Bitmap(MainBox.Width, MainBox.Height, Imaging.PixelFormat.Format32bppArgb)
        Dim BackG As Graphics = Graphics.FromImage(BackBmp)
        BackG.Clear(Color.Transparent)
        If M_BorderStyle = BorderShapeEnum.Square Then
            X = M_BorderWidth
            Y = M_BorderWidth
            X2 = BackBmp.Width - (M_BorderWidth * 2)
            Y2 = BackBmp.Height - (M_BorderWidth * 2)
            BackG.FillRectangle(New SolidBrush(m_BackColor), X, Y, X2, Y2)
        Else
            BackG.FillEllipse(New SolidBrush(m_BackColor), 0 + M_BorderWidth, 0 + M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BackG.FillEllipse(New SolidBrush(m_BackColor), BackBmp.Width - (M_BorderRadius * 2) - M_BorderWidth, 0 + M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BackG.FillEllipse(New SolidBrush(m_BackColor), 0 + M_BorderWidth, BackBmp.Height - (M_BorderRadius * 2) - M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BackG.FillEllipse(New SolidBrush(m_BackColor), BackBmp.Width - (M_BorderRadius * 2) - M_BorderWidth, BackBmp.Height - (M_BorderRadius * 2) - M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BackG.FillRectangle(New SolidBrush(m_BackColor), 0 + M_BorderWidth, M_BorderRadius + M_BorderWidth, BackBmp.Width - (M_BorderWidth * 2), BackBmp.Height - (M_BorderRadius * 2) - (M_BorderWidth * 2))
            BackG.FillRectangle(New SolidBrush(m_BackColor), M_BorderRadius + M_BorderWidth, 0 + M_BorderWidth, BackBmp.Width - (M_BorderRadius * 2) - (M_BorderWidth * 2), BackBmp.Height - (M_BorderWidth * 2))
        End If

        G.DrawImage(FadeBitmap(BackBmp, M_BackOpacity), MainBox.X, MainBox.Y)

        'Create Border BMP
        Dim BorderBmp As New Bitmap(MainBox.Width, MainBox.Height, Imaging.PixelFormat.Format32bppArgb)
        Dim BorderG As Graphics = Graphics.FromImage(BorderBmp)
        BorderG.Clear(Color.Transparent)
        If M_BorderStyle = BorderShapeEnum.Square Then
            BorderG.FillRectangle(New SolidBrush(D_BorderColor), 0, 0, BorderBmp.Width, BorderBmp.Height)
            X = M_BorderWidth
            Y = M_BorderWidth
            X2 = BorderBmp.Width - (M_BorderWidth * 2)
            Y2 = BorderBmp.Height - (M_BorderWidth * 2)
            BorderG.FillRectangle(TransBrush, X, Y, X2, Y2)
            BorderBmp.MakeTransparent(Color.FromArgb(255, 1, 2, 3))
        Else
            If M_ShowText Then
                BorderG.FillRectangle(New SolidBrush(D_BorderColor), 0, 0, M_BorderRadius * 2, M_BorderRadius * 2)
            Else
                BorderG.FillEllipse(New SolidBrush(D_BorderColor), 0, 0, M_BorderRadius * 2, M_BorderRadius * 2)
            End If
            BorderG.FillEllipse(New SolidBrush(D_BorderColor), BorderBmp.Width - (M_BorderRadius * 2), 0, M_BorderRadius * 2, M_BorderRadius * 2)
            BorderG.FillEllipse(New SolidBrush(D_BorderColor), 0, BorderBmp.Height - (M_BorderRadius * 2), M_BorderRadius * 2, M_BorderRadius * 2)
            BorderG.FillEllipse(New SolidBrush(D_BorderColor), BorderBmp.Width - (M_BorderRadius * 2), BorderBmp.Height - (M_BorderRadius * 2), M_BorderRadius * 2, M_BorderRadius * 2)
            BorderG.FillRectangle(New SolidBrush(D_BorderColor), 0, M_BorderRadius, BorderBmp.Width, BorderBmp.Height - (M_BorderRadius * 2))
            BorderG.FillRectangle(New SolidBrush(D_BorderColor), M_BorderRadius, 0, BorderBmp.Width - (M_BorderRadius * 2), BorderBmp.Height)

            BorderG.FillEllipse(New SolidBrush(Color.FromArgb(255, 1, 2, 3)), 0 + M_BorderWidth, 0 + M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BorderG.FillEllipse(New SolidBrush(Color.FromArgb(255, 1, 2, 3)), BorderBmp.Width - (M_BorderRadius * 2) - M_BorderWidth, 0 + M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BorderG.FillEllipse(New SolidBrush(Color.FromArgb(255, 1, 2, 3)), 0 + M_BorderWidth, BorderBmp.Height - (M_BorderRadius * 2) - M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BorderG.FillEllipse(New SolidBrush(Color.FromArgb(255, 1, 2, 3)), BorderBmp.Width - (M_BorderRadius * 2) - M_BorderWidth, BorderBmp.Height - (M_BorderRadius * 2) - M_BorderWidth, M_BorderRadius * 2, M_BorderRadius * 2)
            BorderG.FillRectangle(New SolidBrush(Color.FromArgb(255, 1, 2, 3)), 0 + M_BorderWidth, M_BorderRadius + M_BorderWidth, BorderBmp.Width - (M_BorderWidth * 2), BorderBmp.Height - (M_BorderRadius * 2) - (M_BorderWidth * 2))
            BorderG.FillRectangle(New SolidBrush(Color.FromArgb(255, 1, 2, 3)), M_BorderRadius + M_BorderWidth, 0 + M_BorderWidth, BorderBmp.Width - (M_BorderRadius * 2) - (M_BorderWidth * 2), BorderBmp.Height - (M_BorderWidth * 2))
            BorderBmp.MakeTransparent(Color.FromArgb(255, 1, 2, 3))
        End If
        G.DrawImage(BorderBmp, MainBox.X, MainBox.Y)


        Dim TextBmp As New Bitmap(TopBar.Width, TopBar.Height, Imaging.PixelFormat.Format32bppArgb)
        Dim TextG As Graphics = Graphics.FromImage(TextBmp)
        TextG.Clear(Color.Transparent)
        Dim M_Font As New Font(System.Drawing.FontFamily.GenericSansSerif, M_TextSize, FontStyle.Bold, GraphicsUnit.Pixel)
        Dim Tw As Integer = TextG.MeasureString(M_Text, M_Font).Width
        Dim Th As Integer = TextG.MeasureString(M_Text, M_Font).Height
        If M_BorderStyle = BorderShapeEnum.Square Then
            TextG.FillRectangle(New SolidBrush(M_BorderColor), 0, 0, Tw + 10, TextBmp.Height)
        Else
            TextG.FillEllipse(New SolidBrush(M_BorderColor), 0, 0, (M_BorderRadius * 2), (M_BorderRadius * 2))
            TextG.FillRectangle(New SolidBrush(M_BorderColor), 0, M_BorderRadius, Tw + 10, TextBmp.Height - M_BorderRadius)
            TextG.FillRectangle(New SolidBrush(M_BorderColor), M_BorderRadius, 0, Tw + 10 - M_BorderRadius, TextBmp.Height)

        End If
        Dim pts() As Point = {New Point(Tw + 10, 0), New Point(Tw + 10, TextBmp.Height), New Point(Tw + 10 + TopBar.Height, TextBmp.Height)}
        TextG.FillPolygon(New SolidBrush(M_BorderColor), pts)
        TextG.DrawString(M_Text, M_Font, New SolidBrush(M_TextColor), 5, (TextBmp.Height / 2) - (Th / 2))
        If M_ShowText Then G.DrawImage(FadeBitmap(TextBmp, M_BorderOpacity), TopBar.X, TopBar.Y)

        If M_Debug = True Then
            G.FillRectangle(Brushes.AliceBlue, TLBox)
            G.FillRectangle(Brushes.AliceBlue, TRBox)
            G.FillRectangle(Brushes.AliceBlue, BLBox)
            G.FillRectangle(Brushes.AliceBlue, BRBox)
            G.FillRectangle(Brushes.Brown, TopBar)
            G.FillRectangle(Brushes.Brown, LeftBar)
            G.FillRectangle(Brushes.Brown, RightBar)
            G.FillRectangle(Brushes.Brown, BottomBar)
            G.FillRectangle(Brushes.Coral, MainBox)
            Me.BackColor = Color.Turquoise
        Else
            Me.BackColor = Color.Transparent
        End If
        MyBase.BackgroundImage = MainBmp
    End Sub
    Private Function FadeBitmap(ByVal bmp As Bitmap, ByVal OpacityPercent As Single) As Bitmap
        Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
        OpacityPercent = Math.Min(OpacityPercent, 100)

        Using ia As New Imaging.ImageAttributes
            Dim cm As New Imaging.ColorMatrix
            cm.Matrix33 = OpacityPercent / 100.0F
            ia.SetColorMatrix(cm)
            Dim destpoints() As PointF = _
                   {New Point(0, 0), New Point(bmp.Width, 0), New Point(0, bmp.Height)}
            Using g As Graphics = Graphics.FromImage(bmp2)
                g.Clear(Color.Transparent)
                g.DrawImage(bmp, destpoints, _
                   New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
            End Using
        End Using
        Return bmp2
    End Function
    Private Sub SetupSize()
        Dim Width As Integer = Me.Width
        Dim Height As Integer = Me.Height
        Dim X, Y, H, W As Integer
        'TL Box
        X = M_Border
        Y = M_Border
        W = M_BarSize_Left
        H = M_BarSize_Top
        TLBox = New Rectangle(X, Y, W, H)
        'TR Box
        X = Width - M_Border - M_BarSize_Right
        Y = M_Border
        W = M_BarSize_Right
        H = M_BarSize_Top
        TRBox = New Rectangle(X, Y, W, H)
        'BL Box
        X = M_Border
        Y = Height - M_Border - M_BarSize_Bottom
        W = M_BarSize_Left
        H = M_BarSize_Top
        BLBox = New Rectangle(X, Y, W, H)
        'BR Box
        X = Width - M_Border - M_BarSize_Right
        Y = Height - M_Border - M_BarSize_Bottom
        W = M_BarSize_Right
        H = M_BarSize_Top
        BRBox = New Rectangle(X, Y, W, H)
        'Top Bar
        X = TLBox.Right
        Y = M_Border
        W = TRBox.Left - TLBox.Right
        H = M_BarSize_Top
        TopBar = New Rectangle(X, Y, W, H)
        'Left Bar
        X = M_Border
        Y = TLBox.Bottom
        W = M_BarSize_Left
        H = BLBox.Top - TLBox.Bottom
        LeftBar = New Rectangle(X, Y, W, H)
        'Right Bar
        X = Width - M_Border - M_BarSize_Right
        Y = TRBox.Bottom
        W = M_BarSize_Right
        H = BRBox.Top - TRBox.Bottom
        RightBar = New Rectangle(X, Y, W, H)
        'Bottom Bar
        X = BLBox.Right
        Y = BLBox.Top
        W = BRBox.Left - BLBox.Right
        H = M_BarSize_Bottom
        BottomBar = New Rectangle(X, Y, W, H)

        'Main Box
        X = TLBox.Right
        Y = TLBox.Bottom
        W = TopBar.Width
        H = LeftBar.Height
        MainBox = New Rectangle(X, Y, W, H)


    End Sub
    Private Sub OSDContainer_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        DrawBackground()
    End Sub
    Public Property DebugMode() As Boolean
        Get
            Return M_Debug
        End Get
        Set(ByVal value As Boolean)
            M_Debug = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Bars")> Public Property BarSize_Left() As Integer
        Get
            Return M_BarSize_Left
        End Get
        Set(ByVal value As Integer)
            M_BarSize_Left = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Bars")> Public Property BarSize_Right() As Integer
        Get
            Return M_BarSize_Right
        End Get
        Set(ByVal value As Integer)
            M_BarSize_Right = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Bars")> Public Property BarSize_Top() As Integer
        Get
            Return M_BarSize_Top
        End Get
        Set(ByVal value As Integer)
            M_BarSize_Top = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Bars")> Public Property BarSize_Bottom() As Integer
        Get
            Return M_BarSize_Bottom
        End Get
        Set(ByVal value As Integer)
            M_BarSize_Bottom = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Border")> Public Property OutSideMargin() As Integer
        Get
            Return M_Border
        End Get
        Set(ByVal value As Integer)
            M_Border = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Border")> Public Property BorderRadius() As Integer
        Get
            Return M_BorderRadius
        End Get
        Set(ByVal value As Integer)
            M_BorderRadius = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Border")> Public Property BorderWidth() As Integer
        Get
            Return M_BorderWidth
        End Get
        Set(ByVal value As Integer)
            M_BorderWidth = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Border")> Public Overloads Property BorderStyle() As BorderShapeEnum
        Get
            Return M_BorderStyle
        End Get
        Set(ByVal value As BorderShapeEnum)
            M_BorderStyle = value
            MyBase.BorderStyle = Windows.Forms.BorderStyle.None
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Colors")> Public Property BackOpacity() As Integer
        Get
            Return M_BackOpacity
        End Get
        Set(ByVal value As Integer)
            M_BackOpacity = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Colors")> Public Property BorderOpacity() As Integer
        Get
            Return M_BorderOpacity
        End Get
        Set(ByVal value As Integer)
            M_BorderOpacity = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Colors")> Public Property BorderColor() As Color
        Get
            Return M_BorderColor
        End Get
        Set(ByVal value As Color)
            M_BorderColor = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Colors")> Public Property BackGroundColor() As Color
        Get
            Return m_BackColor
        End Get
        Set(ByVal value As Color)
            m_BackColor = value
            MyBase.BackColor = Color.Transparent
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Caption")> <System.ComponentModel.Browsable(True)> Public Overrides Property Text() As String
        Get
            Return M_Text
        End Get
        Set(ByVal value As String)
            M_Text = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Caption")> Public Property TextColor() As Color
        Get
            Return M_TextColor
        End Get
        Set(ByVal value As Color)
            M_TextColor = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Caption")> Public Property ShowText() As Boolean
        Get
            Return M_ShowText
        End Get
        Set(ByVal value As Boolean)
            M_ShowText = value
            DrawBackground()
        End Set
    End Property
    <System.ComponentModel.Category("Caption")> Public Property TextSize() As Integer
        Get
            Return M_TextSize
        End Get
        Set(ByVal value As Integer)
            M_TextSize = value
            DrawBackground()
        End Set
    End Property

End Class
